package com.njwb.Util;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Io {

    // 导入供应商
    public void importSupplier(Component parent, UtilService utilService) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("选择供应商文件");
        int result = fileChooser.showOpenDialog(parent);
        if (result != JFileChooser.APPROVE_OPTION) return;

        File file = fileChooser.getSelectedFile();

        Pattern pattern = Pattern.compile(
                "供应商名称：(.*?)\\s+供应商描述：(.*?)\\s+联系人：(.*?)\\s+电话：(.*?)\\s+地址：(.*)"
        );

        int successCount = 0;
        int failCount = 0;

        try (Scanner scanner = new Scanner(file, "UTF-8")) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine().trim();
                if (line.isEmpty()) continue;

                Matcher matcher = pattern.matcher(line);
                if (matcher.matches()) {
                    String name = matcher.group(1).trim();
                    String description = matcher.group(2).trim();
                    String contact = matcher.group(3).trim();
                    String phone = matcher.group(4).trim();
                    String address = matcher.group(5).trim();

                    boolean success = utilService.addSupplier(name, description, contact, phone, address);
                    if (success) successCount++;
                    else failCount++;
                } else {
                    failCount++;
                }
            }
            JOptionPane.showMessageDialog(parent, "导入完成！成功：" + successCount + "，失败：" + failCount);
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(parent, "导入失败！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    // 导出表格
    public void exportTableToTxt(Component parent, javax.swing.table.TableModel tableModel) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("保存账单信息");
        fileChooser.setSelectedFile(new File("bills.txt"));
        int result = fileChooser.showSaveDialog(parent);
        if (result != JFileChooser.APPROVE_OPTION) return;

        try (java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(fileChooser.getSelectedFile()))) {
            for (int i = 0; i < tableModel.getColumnCount(); i++) {
                writer.write(tableModel.getColumnName(i) + "\t");
            }
            writer.newLine();

            for (int row = 0; row < tableModel.getRowCount(); row++) {
                for (int col = 0; col < tableModel.getColumnCount(); col++) {
                    writer.write(String.valueOf(tableModel.getValueAt(row, col)) + "\t");
                }
                writer.newLine();
            }
            JOptionPane.showMessageDialog(parent, "导出成功：" + fileChooser.getSelectedFile().getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(parent, "导出失败！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}
